package cn.itrip.biz.service.impl;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.beans.pojo.ItripHotelTempStore;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.pojo.ItripUserLinkUser;
import cn.itrip.beans.vo.ItripHotelOrderExtend;
import cn.itrip.beans.vo.OrderVo;
import cn.itrip.biz.service.ItripHotelOrderService;
import cn.itrip.common.DtoUtils;
import cn.itrip.common.PageUtil;
import cn.itrip.common.RedisUtil;
import cn.itrip.dao.mapper.ItripHotelOrderMapper;
import cn.itrip.dao.mapper.ItripHotelTempStoreMapper;
import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.internal.objects.NativeUint8Array;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
@Component
public class ItripHotelOrderServiceImpl implements ItripHotelOrderService {
    Logger logger  =  Logger.getLogger(ItripUserLinkUserMapperServiceImpl.class);
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private HttpServletRequest request;
    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;

    @Override
    public Dto selectOrderAll(OrderVo orderVo) {

        //获取请求头token
        String token=request.getHeader("token");
        //根据获取的key获取redis中对应的value
        String key=redisUtil.getValue(token);
        //把JSON转换成ItripUser对象
        ItripUser user= JSONObject.parseObject(key,ItripUser.class);
            try {
                //设置默认第一页
                int index=1;
                //判断页面是否大于0
                if (orderVo.getPageNo()>0){
                    //设置当前页
                    index=orderVo.getPageNo();
                }
                //查询总数量
                PageUtil page=new PageUtil(itripHotelOrderMapper.selectCount(orderVo,(int)user.getId()),index);
                page.setQueryData(orderVo);
                //显示订单信息
                List<ItripHotelOrder> list=itripHotelOrderMapper.selectUserByQuery(page,(int)user.getId());
                page.setRows(list);
                if (null!=list){
                    return DtoUtils.getSuccesDto(page);
                }
                return DtoUtils.getErrorDto("失败","40000");
            }catch (Exception e){
                //打印异常信息
                logger.debug("出错了"+e);
                return DtoUtils.getExcetpionDto(e,"30002");
            }

        }

    @Override
    public Dto selectById(String id){
        ItripHotelOrderExtend itripHotelOrder=null;
        try {

            itripHotelOrder = (ItripHotelOrderExtend)itripHotelOrderMapper.selectById(Long.parseLong(id));

            System.out.println("123");
        } catch (Exception e) {
            //打印异常信息
            logger.debug("出错了"+e);
            return DtoUtils.getExcetpionDto(e,"30002");
        }
        itripHotelOrder.setProcessNode(itripHotelOrder.getOrderStatus()+2+"");
        if(itripHotelOrder.getOrderStatus()==2){
            itripHotelOrder.setProcessNode(itripHotelOrder.getOrderStatus()+1+"");
        }
        if(null!=itripHotelOrder){
            return DtoUtils.getSuccesDto(itripHotelOrder);
        }
        return DtoUtils.getSuccesDto(null);
    }

    @Override
    public Dto selectByKey(String id){
        ItripHotelOrder itripHotelOrder= null;
        try {
            itripHotelOrder = itripHotelOrderMapper.selectByKey(Long.parseLong(id));
        } catch (Exception e) {
            //打印异常信息
            logger.debug("出错了"+e);
            return DtoUtils.getExcetpionDto(e,"30002");
        }
        if (null!=itripHotelOrder){
            return DtoUtils.getSuccesDto(itripHotelOrder);
        }
        return DtoUtils.getSuccesDto(null);
    }
    @Scheduled(cron="0/10 * *  * * ? ")
    @Override
    public void timer() throws Exception {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前系统时间
       Date sj= new Date();
       //获取所有订单待付款的信息
        List<ItripHotelOrder> list=itripHotelOrderMapper.selectAll();
      for(ItripHotelOrder itripHotelOrder:list){
            //获取当前时间减去订单创建时间差
          long diff = sj.getTime() - itripHotelOrder.getCreationDate().getTime();
          //获取小时
          //long hours = (diff)/(1000* 60 * 60);
          //时间差转为分钟
          long minutes = (diff)/(1000* 60);
          //判断订单是否超时
         if(15<=minutes){
             //执行修改订单状态
             int num1=itripHotelTempStoreMapper.updateById(itripHotelOrder.getId());
             if(num1>0){
                 //修改库存
              int num=itripHotelTempStoreMapper.update(itripHotelOrder.getHotelId(),itripHotelOrder.getRoomId());
           }
         }
      }
    }
}
